#cmf 12 Feb 2013

#read in files, add info, spit out new by-subject csvs, spit out new master csv for DYNaming (from MCF lab)
#read in data files from directory (originally rtf, converted to plain txt before running this)
#add columns: age group, subjectID, familiarity category, instance number
#spit out new data file for each subject, named like 0401_data.csv
#concatenate all subjects, write to csv DYNaming_AllData

####################################################################################
#before running, set working directory to wherever the 20 txt files are

#clear workspace
rm(list=ls())

####################################################################################
#allocate master dataframe 
AllData = data.frame()

#anything other than zem, tima, gimo, manu are familiar
Novels = c('zem', 'tima', 'gimo', 'manu','gasser')

####################################################################################
#read all files in working directory
fileList = list.files()

#loop over all files
for(i in 1:length(fileList)){
  dataTemp = read.csv(fileList[i], header = FALSE, sep = " ", colClasses = "character") #read in file, split on space character, dont make strings 'factor' datatype
  dataTemp = dataTemp[,-3] #get rid of third column, currently empty (because split on ' ', above)
  subID = strsplit(fileList[i], '_')[[1]][2] #grab subID from the filename
  subIDColumn = rep(subID, dim(dataTemp)[1]) #repeat subID for length of file 
  AgeGroup = substr(subID,1,2) #grab first two characters of subID
  AgeGroupColumn = rep(AgeGroup, dim(dataTemp)[1]) #repeat AgeGroup for length of file
  InstanceNums = seq(1:dim(dataTemp)[1]) #list the rows
  Familiarity = rep('NA', dim(dataTemp)[1]) #gonna fill in this column below
  dataTemp = cbind(AgeGroupColumn, subIDColumn, InstanceNums, dataTemp, Familiarity) #bind all columns together
  colnames(dataTemp) = c('AgeGroup', 'SubjectID', 'InstanceNum', 'Name', 'Timestamp', 'Familiarity') #name the columns

  #stupid data type issues. make sure character. 
  dataTemp$AgeGroup = as.character(dataTemp$AgeGroup)
  dataTemp$SubjectID = as.character(dataTemp$SubjectID)
  dataTemp$Familiarity = as.character(dataTemp$Familiarity)

  #populate the Familiarity column
  dataTemp$Familiarity[dataTemp$Name %in% Novels] = 'Novel'
  dataTemp$Familiarity[!dataTemp$Name %in% Novels] = 'Familiar'

  #a few input txt files have empty row at end. check if Timestamp column is empty, if so, delete last row
  if(dataTemp$Timestamp[dim(dataTemp)[1]] == ''){
    dataTemp = dataTemp[-dim(dataTemp)[1],]
  }#end-if
  
  #write each updated subject file to csv. leading zero on subID and ageGroup is dropped (no info lost, so not gonna sweat it right now)
  write.csv(dataTemp, paste(subID, 'data.csv', sep = '_'), row.names = FALSE)
  
  #add to master data frame
  AllData = rbind(AllData, dataTemp)
  
  #clear some temp variables, just cuz i can....
  rm(dataTemp); rm(Familiarity); rm(InstanceNums); rm(subID); rm(subIDColumn); rm(AgeGroup); rm(AgeGroupColumn)
} #end-for

####################################################################################
#write master file to csv
#leading 0: lost for subID, not a problem for timestamp (surrounded by #  #)
write.csv(AllData, "DYNaming_AllData.csv", row.names = FALSE)




